package com.flin.icesilkworm.examination.api;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import com.flin.icesilkworm.examination.bas.model.TheBasicExaminee;
import com.flin.icesilkworm.examination.bas.model.TheBasicTeacher;
import com.flin.icesilkworm.examination.bas.po.TheBasicExamineeDo;
import com.flin.icesilkworm.examination.bas.po.TheBasicTeacherDo;
import com.flin.icesilkworm.examination.course.model.CourseLearnRecord;
import com.flin.icesilkworm.examination.course.po.CourseLearnRecordDo;
import com.flin.icesilkworm.framework.logs.SybrisLog;
import com.flin.icesilkworm.framework.spring.api.ApiSupport;
import com.flin.icesilkworm.framework.spring.api.IOpenApiSupport;
import com.flin.icesilkworm.framework.spring.api.IRequestConvertTo;

/**
 * 定义客户端请求的转换逻辑
 * @author flin
 *
 */
@Component
public class OpenApiSupportWrapper extends ApiSupport implements IOpenApiSupport{

	SybrisLog log = new SybrisLog(this.getClass());
	
	@Autowired
	private RedisTemplate<String, String> redisTemplate;
	
	
	@PostConstruct
	public void init(){
	}

	@SuppressWarnings("serial")
	private Map<Class<?>, IRequestConvertTo> converts = new HashMap<Class<?>, IRequestConvertTo> (){{
		put(TheBasicExaminee.class,(user,uid,req,resp,i, params,  oa)->user);
		put(TheBasicTeacher.class,(user,uid,req,resp,i, params,  oa)->user);
		put(CourseLearnRecord.class,(user,uid,req,resp,i, params,  oa)->{
			String proId = req.getParameter("proId");
			if(proId != null){
				return CourseLearnRecordDo.get().examId(uid).proId(Long.parseLong(proId)).deleted(Boolean.FALSE).re().findForWhere();
			}
			return null;
		});
	 
		 
		
		
	}};
	
	public Map<Class<?>, IRequestConvertTo> getConverts() {
		
		
		return converts;
	}

	public Long getId(Object user) {
		if(user instanceof TheBasicTeacher) return ((TheBasicTeacher) user).getTeachId();
		return ((TheBasicExaminee)user).getExameeId();
	}

	public boolean isBackResultOutPackage() {
		return true;
	}

	public void definedBean(Map<String, Class<?>> definedBean) {
		
	}

	public Object getToken(String token, String _c, String _m) {
		if(token == null || "rebot".equals(_c)) return null;
		//登录缓存半个小时
		if("exam".equals(_c)){
			TheBasicExaminee user = TheBasicExamineeDo.get().theExameeWxOpenId(token).re().findForWhere();
			return user;
		}
		if("teacher".equals(_c)){
			TheBasicTeacher user = TheBasicTeacherDo.get().teacherCode(token).re().findForWhere();
			return user;
		}
		return null;
	}

	public boolean hasToken(String _c, String _m) {
		 
		return true;
	}

}
